//
//  RAPStatusCellOriginalView.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/1/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPStatusCellOriginalView: UIView {

    var statusCellOriginalViewBottomConstraint: Constraint?
    
    // 重写UIView的指定构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        addSubview(profile_image_url_ImageView)
        addSubview(created_at_Label)
        addSubview(source_Label)
        addSubview(verified_type_ImageView)
        addSubview(mbrank_ImageView)
        addSubview(screen_name_Label)
        addSubview(text_Label)
        addSubview(retweetPicturesView)
        
        installConstraints()
        
    }
    
    func installConstraints() {
        profile_image_url_ImageView.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(kStatusCellMargin)
            make.height.equalTo(35)
            make.width.equalTo(35)
        }
        
        screen_name_Label.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(profile_image_url_ImageView.snp_trailing).offset(kStatusCellMargin)
            make.top.equalTo(profile_image_url_ImageView)
        }
        
        mbrank_ImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screen_name_Label.snp_trailing).offset(kStatusCellPadding)
            make.centerY.equalTo(screen_name_Label)
        }
        
        created_at_Label.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(profile_image_url_ImageView.snp_trailing).offset(kStatusCellMargin)
            make.bottom.equalTo(profile_image_url_ImageView)
        }
        
        source_Label.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(created_at_Label.snp_trailing).offset(kStatusCellPadding)
            make.centerY.equalTo(created_at_Label)
        }
        
        verified_type_ImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(profile_image_url_ImageView.snp_trailing).offset(-kStatusCellPadding)
            make.centerY.equalTo(profile_image_url_ImageView.snp_bottom).offset(-kStatusCellPadding)
        }
        
        text_Label.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(profile_image_url_ImageView)
            make.width.equalTo(self).offset(-2 * kStatusCellMargin)
            make.top.equalTo(profile_image_url_ImageView.snp_bottom).offset(kStatusCellMargin)
            make.bottom.lessThanOrEqualTo(self)
        }
        
        retweetPicturesView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(text_Label.snp_bottom).offset(kStatusCellMargin)
            make.leading.equalTo(text_Label)
            
//                        make.height.equalTo(150) //
//                        make.width.equalTo(self).offset(-2 * kStatusCellMargin) //
        }
        
        self.snp_makeConstraints { (make) -> Void in // 已经修改
            self.statusCellOriginalViewBottomConstraint = make.bottom.equalTo(retweetPicturesView).offset(kStatusCellMargin).constraint
        }
    }

    /*
     如果调用了从xib创建UIView的构造器，则会报错，提示这个方法无法达到和init(frame:)相同的效果
    */
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusCellViewModel: RAPStatusCellViewModel? {
        didSet {
            
            if let statusModel = statusCellViewModel?.statusModel {
                profile_image_url_ImageView.sd_setImageWithURL(NSURL(string: (statusModel.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_small"))
                screen_name_Label.text = statusModel.user?.screen_name
//                created_at_Label.text = statusModel.created_at
//                source_Label.text = statusModel.source
                text_Label.text = statusModel.text
                
                if let img = statusCellViewModel?.mbRank_ImageName {
                    mbrank_ImageView.image = UIImage(named: img)
                }
                
                if let img = statusCellViewModel?.verified_type_ImageName {
                    verified_type_ImageView.image = UIImage(named: img)
                }
                
                if let text = statusCellViewModel?.source_Text {
                    source_Label.text = text
                }
                
                if let text = statusCellViewModel?.created_at_Text {
                    created_at_Label.text = text
                }
                
                // 设置原创配图gif
                retweetPicturesView.isGIFs = statusCellViewModel?.isOriPicGIFs
                
                
                // 卸载约束
                statusCellOriginalViewBottomConstraint?.uninstall()
                if let picURLs = statusModel.pic_urls where picURLs.count > 0 {
                    // 有配图
                    self.retweetPicturesView.hidden = false
                    retweetPicturesView.pic_urls = picURLs
                    
                    self.snp_makeConstraints(closure: { (make) -> Void in
                        self.statusCellOriginalViewBottomConstraint = make.bottom.equalTo(retweetPicturesView).offset(kStatusCellMargin).constraint
                    })
                } else {
                    // 无配图
                    self.retweetPicturesView.hidden = true
                    self.snp_makeConstraints(closure: { (make) -> Void in
                        self.statusCellOriginalViewBottomConstraint = make.bottom.equalTo(text_Label).offset(kStatusCellMargin).constraint
                    })
                }

                
            }
        }
    }
    
    
    // MARK: - 懒加载控件
    lazy var profile_image_url_ImageView: UIImageView = {
        let profile_image_url_ImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
        return profile_image_url_ImageView
    }()
    
    
    lazy var screen_name_Label: UILabel = {
        let screen_name_Label = UILabel(text: "哈哈", fontSize: 14)
//        screen_name_Label.backgroundColor = UIColor.whiteColor()
        return screen_name_Label
    }()
    
    lazy var created_at_Label: UILabel = {
        let created_at_Label = UILabel(text: "123445667789", fontSize: 10, textColor: UIColor.lightGrayColor())
        return created_at_Label
    }()
    
    lazy var source_Label: UILabel = {
        let source_Label = UILabel(text: "发自我的小米26", fontSize: 10, textColor: UIColor.lightGrayColor())
        return source_Label
    }()
    

    lazy var verified_type_ImageView: UIImageView = {
//        let verified_type_ImageView = UIImageView(image: UIImage(named: "avatar_vip"))
        let verified_type_ImageView = UIImageView()
        
        return verified_type_ImageView
    }()
    
    lazy var mbrank_ImageView: UIImageView = {
//        let mbrank_ImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
        let mbrank_ImageView = UIImageView()
        
        return mbrank_ImageView
    }()
    
    lazy var text_Label: UILabel = {
        let text_Label = UILabel(text: "呼呼呼受到核辐射的回复速度回复史蒂夫华盛顿恢复速度回复速度回复速度回复速度回复速度回复速度回复速度和粉红色的", fontSize: 13)
        text_Label.numberOfLines = 0
        return text_Label
    }()
    
    /**
    转发微博配图
    */
    lazy var retweetPicturesView: RAPStatusCellPicturesView = {
        let retweetPicturesView = RAPStatusCellPicturesView()
        
        return retweetPicturesView
        }()
}
